package com.yjd.back.notice;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.notice.model.NoticeModel;
import com.yjd.comm.notice.service.INoticeService;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by yjd-wap on 2018/5/2.
 */

@Controller
@Scope("prototype")
@RequestMapping("/notice/")
public class NoticeControl extends BaseControl {

    @Resource
    private INoticeService noticeService;

    /**
     * 公告列表
     * @return void
     * @throws
     */
    @RequestMapping(value = "list")
    public String list() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("sort", "create_time desc");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.noticeService.getPageModel(paramMap, DbWREnums.READ), paramMap,
                ServletUtil.getUrl("/notice/list"));

        List<NoticeModel> noticeModels = pager.getPagerModel().getDataList();
        noticeModels.forEach(noticeModel -> {
            noticeModel.getStrMap().put("create_time",DateUtil.timestampToDateString(noticeModel.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            noticeModel.getStrMap().put("update_time",DateUtil.timestampToDateString(noticeModel.getUpdate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            noticeModel.getStrMap().put("start_time",DateUtil.timestampToDateString(noticeModel.getStart_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            noticeModel.getStrMap().put("end_time",DateUtil.timestampToDateString(noticeModel.getEnd_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        });

        request.setAttribute("maps", paramMap);
        request.setAttribute("pager", pager);

        return "notice.list";
    }

    /**
     * 发布公告
     * @return void
     * @throws
     */
    @RequestMapping(value = "add")
    public String addNoticePage() throws Exception {
        return "notice.add";
    }

    /**
     * 编辑页面
     * @return void
     * @throws
     */
    @RequestMapping(value = "edit")
    public String editdNoticePage(Long id) throws Exception {
        NoticeModel noticeModel = noticeService.getModelById(id, DbWREnums.READ);
        if(noticeModel!=null){
            noticeModel.getStrMap().put("start_time_str",DateUtil.timestampToDateString(noticeModel.getStart_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            noticeModel.getStrMap().put("end_time_str",DateUtil.timestampToDateString(noticeModel.getEnd_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        }
        request.setAttribute("noticeModel", noticeModel);
        return "notice.add";
    }

    /**
     * 保存公告
     * @return void
     * @throws
     */
    @RequestMapping(value = "/add/submit")
    public void saveNotice(NoticeModel noticeModel) throws Exception {
        if(StringUtil.isEmpty(noticeModel.getTitle())){
            FrameUtil.throwBaseException1("公告标题不能为空");
        }

        if(StringUtil.isEmpty(noticeModel.getContent())){
            FrameUtil.throwBaseException1("公告内容不能为空");
        }

        String start_time_str =  request.getParameter("start_time_str");
        String end_time_str =  request.getParameter("end_time_str");

        if(StringUtil.isEmpty(start_time_str)){
            FrameUtil.throwBaseException1("公告开始时间不能为空");
        }

        if(StringUtil.isEmpty(end_time_str)){
            FrameUtil.throwBaseException1("公告截止时间不能为空");
        }

        Long start_time = DateUtil.dateStringToTimestamp(start_time_str,DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss);
        Long end_time = DateUtil.dateStringToTimestamp(end_time_str,DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss);

        noticeModel.setStart_time(start_time);
        noticeModel.setEnd_time(end_time);

        Long currtime = FrameUtil.getTime();
        if(noticeModel.getId()==null){
            noticeModel.setCreate_time(currtime);
            noticeModel.setUpdate_time(currtime);
            noticeService.insert(noticeModel);
        }else {
            noticeModel.setUpdate_time(currtime);
            noticeService.update(noticeModel);
        }

        this.successMsg(ServletUtil.getUrl("/notice/list"), "保存公告成功!");
    }

}
